﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MagicCard
{
    public partial class frmCompositeMgmt : Form
    {
        private string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MagicCard.accdb";
        private OleDbConnection conn = null;
        private AccessDao accessDao = null;

        public frmCompositeMgmt()
        {
            InitializeComponent();
        }

        private void frmCompositeMgmt_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(connStr);
            accessDao = new AccessDao(conn);
            Init();
        }

        private void Init()
        {
            this.cboCardLv.SelectedIndex = 0;

            this.cboStars.DataSource = accessDao.GetStarsFromCategory();
            this.cboStars.DisplayMember = "Stars";

            this.GetCategorys();

            this.GetBaseCards();

            this.GetCardInfo();
        }

        private void GetCategorys()
        {
            this.cboCategory.DataSource = accessDao.GetCategoryByStars(this.cboStars.Text.Trim());
            this.cboCategory.DisplayMember = "CategoryName";
            this.cboCategory.ValueMember = "ID";
        }

        private void GetBaseCards()
        {
            DataTable baseCards1 = accessDao.GetBaseCardsForComposite(this.cboCategory.Text.Trim(), this.cboCardLv.SelectedIndex + 1);
            if (baseCards1.Rows.Count > 0)
            {
                DataTable baseCards2 = baseCards1.Copy();
                DataTable baseCards3 = baseCards1.Copy();

                this.cboBase1.DataSource = baseCards1;
                this.cboBase1.DisplayMember = "CardName";
                this.cboBase1.ValueMember = "ID";

                this.cboBase2.DataSource = baseCards2;
                this.cboBase2.DisplayMember = "CardName";
                this.cboBase2.ValueMember = "ID";

                this.cboBase3.DataSource = baseCards3;
                this.cboBase3.DisplayMember = "CardName";
                this.cboBase3.ValueMember = "ID";
            }
            else
            {
                //MessageBox.Show("该套卡无任何素材卡，请先增加素材卡！", "警告");
                //return;
            }
        }

        private void GetCardInfo()
        {
            int id = int.Parse(this.cboCategory.SelectedValue.ToString());
            this.grdCardInfo.DataSource = accessDao.GetCardInfoByCategory(id);
        }

        private void cboStars_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GetCategorys();
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GetBaseCards();

            this.GetCardInfo();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int cardValue;
            if (!int.TryParse(txtCardValue.Text.Trim(), out cardValue))
            {
                MessageBox.Show("卡片面值必须是整数", "错误");
                return;
            }
            if (txtCardName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("卡片名称不能为空，请重新输入！", "错误");
                return;
            }
            if (accessDao.IsDoubleName(txtCardName.Text.Trim()))
            {
                MessageBox.Show("卡片名称已经存在，请重新输入！", "错误");
                return;
            }
            int time = ParseTime(this.txtHH.Text.Trim(), this.txtMM.Text.Trim());
            if (time == 0)
            {
                MessageBox.Show("炼制时间不能为零！", "错误");
                return;
            }

            StringBuilder commStr = new StringBuilder();
            commStr.Append("INSERT INTO MC_Card  (CardCategory,CardName,CardLv,Requirements,CardValue,CardTime,Base1,Base2,Base3)  values");
            commStr.Append("(" + this.cboCategory.SelectedValue);
            commStr.Append(",'" + txtCardName.Text.Trim() + "'");
            commStr.Append("," + (this.cboCardLv.SelectedIndex + 1));
            commStr.Append(",1");
            commStr.Append("," + cardValue);
            commStr.Append("," + time);
            commStr.Append("," + this.cboBase1.SelectedValue);
            commStr.Append("," + this.cboBase2.SelectedValue);
            commStr.Append("," + this.cboBase3.SelectedValue);
            commStr.Append(")");

            try
            {
                conn.Open();
                OleDbCommand comm = new OleDbCommand(commStr.ToString(), conn);
                int ret = comm.ExecuteNonQuery();
                if (ret == 1)
                {
                    MessageBox.Show("卡片增加成功！", "成功");
                }
                else
                {
                    MessageBox.Show("卡片增加失败！", "失败");
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }

        private int ParseTime(string hours, string mintues)
        {
            int hh, mm;
            int.TryParse(hours, out hh);
            int.TryParse(mintues, out mm);
            return hh * 60 + mm;
        }

        private void cboCardLv_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetBaseCards();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            string message;
            int id = int.Parse(this.cboCategory.SelectedValue.ToString());
            if (!accessDao.Check(id, out message))
            {
                MessageBox.Show(message, "错误");
                return;
            }
            this.accessDao.InitCardInfo(id);
            if (this.accessDao.Calculate(id))
            {
                MessageBox.Show("更新成功！");
                return;
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }
    }
}
